package com.florent3.shapeofview.sample.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import com.florent3.shapeofview.sample.ResourceTable;
import com.github.florent37.shapeofview.shapes.ArcView;

/**
 * ShapeOfViewStarAnimatedAbilitySlice
 *
 * @since 2021-07-26
 */
public class ShapeOfViewStarAnimatedAbilitySlice extends AbilitySlice {
    private static HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x0000259, "ShapeOfViewStarAnimatedAbilitySlice");
    private ArcView arcView;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_shape_of_view_star_animated);
        initView();
    }


    private ValueAnimator v1 = ValueAnimator.ofFloat(0f, -200f);
    private ValueAnimator v2 = ValueAnimator.ofFloat(-200f, 200f);
    private ValueAnimator v3 = ValueAnimator.ofFloat(200f, -200f);
    private ValueAnimator v4 = ValueAnimator.ofFloat(-200f, 0f);

    private class AnimateListenerAdapter implements ValueAnimator.AnimatorListener {

        @Override
        public void onAnimationStart(ValueAnimator animation) {

        }

        @Override
        public void onAnimationEnd(ValueAnimator animation) {

        }

        @Override
        public void onAnimationStop(ValueAnimator animation) {

        }

        @Override
        public void onAnimationCancel(ValueAnimator animation) {

        }

        @Override
        public void onAnimationRepeat(ValueAnimator animation) {

        }

        @Override
        public void onAnimationPause(ValueAnimator animation) {

        }

        @Override
        public void onAnimationResume(ValueAnimator animation) {

        }
    }

    private void initView() {
        arcView = (ArcView) findComponentById(ResourceTable.Id_arcLayout);

        if (arcView != null) {
            ValueAnimator.AnimatorUpdateListener listener = new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(AnimatorValue animator, float fraction, Object animatedValue) {
                    arcView.setArcHeight((Float) animatedValue);
                }
            };
            v1.addUpdateListener(listener);
            v2.addUpdateListener(listener);
            v3.addUpdateListener(listener);
            v4.addUpdateListener(listener);
            v1.addListener(new AnimateListenerAdapter() {
                @Override
                public void onAnimationEnd(ValueAnimator animation) {
                    v2.start();
                }
            });
            v2.addListener(new AnimateListenerAdapter() {
                @Override
                public void onAnimationEnd(ValueAnimator animation) {
                    v3.start();
                }
            });
            v3.addListener(new AnimateListenerAdapter() {
                @Override
                public void onAnimationEnd(ValueAnimator animation) {
                    v4.start();
                }
            });
            v4.addListener(new AnimateListenerAdapter() {
                @Override
                public void onAnimationEnd(ValueAnimator animation) {
                    v1.start();
                }
            });
            v1.setDuration(1250);
            v2.setDuration(1250);
            v3.setDuration(1250);
            v4.setDuration(1250);
            v1.start();
        }
    }

    /**
     * get the animated value with fraction and values
     *
     * @param fraction 0~1
     * @param values float array
     * @return float animated value
     */
    public static float getAnimatedValue(float fraction, float... values) {
        if (values == null || values.length == 0) {
            return 0;
        }
        if (values.length == 1) {
            return values[0] * fraction;
        } else {
            if (fraction == 1) {
                return values[values.length - 1];
            }
            float oneFraction = 1f / (values.length - 1);
            float offFraction = 0;
            for (int i = 0; i < values.length - 1; i++) {
                if (offFraction + oneFraction >= fraction) {
                    return values[i] + (fraction - offFraction) * (values.length - 1) * (values[i + 1] - values[i]);
                }
                offFraction += oneFraction;
            }
        }
        return 0;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
